package com.Sing.Filter;

import com.Sing.Utils.Constant;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//@WebFilter( filterName = "LoginFilter",urlPatterns = {"/loginIn#/*"})
public class LoginFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println( "Login Filter Start" );
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        HttpServletResponse resp = (HttpServletResponse) response;
        HttpServletRequest req = (HttpServletRequest) request;

        Object attribute = req.getSession().getAttribute( Constant.USER_SESSION );
        if ( attribute != null ){
            doFilter( request, response, chain );
        } else {
            resp.sendRedirect( "/404.jsp" );
        }
    }

    @Override
    public void destroy() {
        System.out.println( "Login Filter Down" );
    }
}
